<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button icon="el-icon-plus" size="small" type="primary" @click="roleDialogVisible = true">新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="list">
          <el-table-column align="center" type="index" :index="indexMethod" label="序号" width="120" />
          <el-table-column align="center" prop="name" label="角色名称" width="240" />
          <el-table-column align="center" prop="description" label="描述" />
          <el-table-column label="操作" width="240" fixed="right">
            <template v-slot="{row}">
              <el-button size="small" @click="showPermission(row.id)">分配权限</el-button>
              <el-button size="small" @click.native="updataRole(row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click.native="delRole(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination
            layout="total, sizes, prev, pager, next, jumper"
            :current-page="queryData.page"
            :total="total"
            :page-size="queryData.pagesize"
            :page-sizes="[5,10,15,20]"
            @size-change="handleSizeChange"
            @current-change="pageChange"
          />
        </el-row>
      </el-card>
    </div>
    <!-- 新增角色弹窗 -->
    <el-dialog title="新增角色" :visible="roleDialogVisible" @close="closeRoleDialog">
      <!-- 表单内容 -->
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-button size="small" @click="roleDialogVisible = false">取消</el-button>
        <el-button size="small" type="primary" @click="confirmAdd">确定</el-button>
      </el-row>
    </el-dialog>
    <!-- 分配权限弹窗 -->
    <el-dialog
      title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
      :visible="showAssignDialog"
      @close="closeAssignDialog"
      @open="getPermissionList"
    >
      <!-- 权限点数据展示 -->
      <el-tree
        ref="refRole"
        :data="permissionList"
        :props="{ label: 'name' }"
        :default-expand-all="true"
        :show-checkbox="true"
        :check-strictly="true"
        node-key="id"
      />
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="closeAssignDialog">取消</el-button>
          <el-button type="primary" @click="clickAssign">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleListAPI, delRoleAPI, addRoleAPI, getRoleAPI, updataRoleAPI, distRoleAPI } from '@/api/role'
import { getPermiissionListAPI } from '@/api/permission'
import transTree from '@/utils/transTree'
export default {
  data() {
    return {
      // 渲染页面
      list: [],
      // 请求角色列表参数
      queryData: {
        page: 1,
        pagesize: 5
      },
      // 角色总数量
      total: 0,
      // 控制弹窗显示和隐藏
      roleDialogVisible: false,
      // 新增角色信息
      roleForm: {
        name: '',
        description: ''
      },
      // 验证信息
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      // 控制分配角色弹窗显示和隐藏
      showAssignDialog: false,
      permissionList: [], // 存储权限数据
      // 分配角色的数据
      AssaginRole: {
        id: '',
        permIds: []
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取角色列表
    async getList() {
      const { rows, total } = await getRoleListAPI(this.queryData)
      this.list = rows
      this.total = total
    },
    // 切换页面
    pageChange(page) {
      // console.log(page)
      this.queryData.page = page
      this.getList()
    },
    // 删除角色
    delRole(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        type: 'warning'
      }).then(async() => {
        await delRoleAPI(id)
        this.$message.success('删除成功')
        this.getList()
      })
    },
    // 关闭弹窗
    closeRoleDialog() {
      this.roleDialogVisible = false
      this.$refs.roleForm.resetFields()
      this.resetFrom()
    },
    // 添加角色 或者 编辑角色
    confirmAdd() {
      this.$refs.roleForm.validate(async(valid) => {
        if (!valid) return
        if (this.roleForm.id) {
          await updataRoleAPI(this.roleForm)
        } else {
          await addRoleAPI(this.roleForm)
        }
        this.$message.success(`${this.roleForm.id ? '编辑' : '添加'}成功`)
        this.getList()
        this.roleDialogVisible = false
      })
    },
    // 获取角色详情
    async updataRole(id) {
      this.roleDialogVisible = true
      const res = await getRoleAPI(id)
      this.roleForm = res
    },
    // 清空表单的方法
    resetFrom() {
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 分页中每页多少条数据
    handleSizeChange(val) {
      this.queryData.pagesize = val
      this.getList()
    },
    // 序号变成连续的
    indexMethod(index) {
      // return index + 1 + (this.当前页面 - 1) * this.每页显示的条数
      return index + 1 + (this.queryData.page - 1) * this.queryData.pagesize
    },
    // 打开添加权限的弹窗
    async showPermission(id) {
      this.showAssignDialog = true
      this.AssaginRole.id = id
      const res = await getRoleAPI(id)
      this.$refs.refRole.setCheckedKeys(res.permIds)
    },
    // 关闭添加权限的弹窗
    closeAssignDialog() {
      this.showAssignDialog = false
      this.$refs.refRole.setCheckedKeys([])
    },
    // 打开弹窗获取权限点列表数据
    async getPermissionList() {
      const res = await getPermiissionListAPI()
      this.permissionList = transTree(res)
    },
    // 点击确定添加权限点
    async clickAssign() {
      this.AssaginRole.permIds = this.$refs.refRole.getCheckedKeys()
      await distRoleAPI(this.AssaginRole)
      this.$message.success('分配权限成功')
      this.showAssignDialog = false
    }
  }
}
</script>
